package unit8.part6;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Vector;
import java.util.List;

public class InfoQuery {
    static String[] columnNames = {"员工编号", "员工姓名", "性  别", "出生日期", "部  门", "职  务", "手机号码"};
    static String[][] infos = new String[][]{{"AL001", "李名", "男", "1985.2", "设计部", "设计师", "13588932189"},
            {"AL002", "王百", "男", "1989.1", "设计部", "设计师", "18988792178"},
            {"AL003", "沈琪", "女", "1995.7", "美工部", "美工师", "15808978329"},
            {"AL004", "来福", "男", "1998.2", "美工部", "美工师", "18688933137"},
            {"AL005", "方莱", "女", "1999.3", "市场部", "业务员", "15688932125"},
            {"AL006", "郎军", "男", "1997.2", "市场部", "业务员", "13588932139"}};
    static Font font = new Font("微软雅黑", Font.BOLD, 12);

    public static void main(String[] args) {
        MainFrame mainFrame = new MainFrame();
        mainFrame.setVisible(true);
    }
}

class SearchConditionPanel extends JPanel {// 查询条件面板
    JButton searchAll;// 查询全部按钮
    JButton searchOneDepartment;// 查询部门按钮
    JComboBox<String> chooseDepartment;// 下拉选择框

    public SearchConditionPanel(ActionListener listener) {
        // 边框
        setBorder(new TitledBorder(null, "查询条件", TitledBorder.LEFT, TitledBorder.TOP, null, null));
        // 布局
        setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

        searchAll = new JButton("查询所有员工信息");// 查询全部按钮
        searchAll.addActionListener(listener);// 监听器
        searchAll.setFont(InfoQuery.font);// 字体
        add(searchAll);// 加入到面板

        searchOneDepartment = new JButton("查询部门员工信息");// 查询部门按钮
        searchOneDepartment.addActionListener(listener);// 监听器
        searchOneDepartment.setFont(InfoQuery.font);// 字体
        add(searchOneDepartment);// 加入到面板

        Vector<String> vector = new Vector<>();// 存储所有部门名字
        for (String[] item : InfoQuery.infos) {
            if (!vector.contains(item[4])) {// 去重
                vector.add(item[4]);
            }
        }
        chooseDepartment = new JComboBox<>(vector);// 选择框
        chooseDepartment.setFont(InfoQuery.font);
        add(chooseDepartment);
    }
}

class SearchResultPanel extends JPanel {// 查询结果面板
    JTable table;// 表格
    JScrollPane scrollPane;// 滚动框

    public SearchResultPanel() {
        // 边框
        setBorder(new TitledBorder(null, "查询结果", TitledBorder.LEFT, TitledBorder.TOP, null, null));

        table = new JTable();// 表格
        scrollPane = new JScrollPane(table);// 滚动框
        add(scrollPane);// 加入到面板
        draw(InfoQuery.infos);
    }

    void draw(String[][] infos) {// 绘制查询结果
        TableModel tableModel = new DefaultTableModel(infos, InfoQuery.columnNames);
        table.setModel(tableModel);
    }
}

class MainFrame extends JFrame implements ActionListener {// 窗体
    SearchConditionPanel searchConditionPanel;// 查询条件面板
    SearchResultPanel searchResultPanel;// 查询结果面板

    public MainFrame() {
        setTitle("查询员工信息");// 标题
        setBounds(100, 100, 553, 339);// 位置和大小
        JPanel contentPane = new JPanel();
        contentPane.setBackground(new Color(245, 245, 245));// 背景色
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));// 边界
        setContentPane(contentPane);// 配置内容面板

        searchConditionPanel = new SearchConditionPanel(this);// 管理面板
        add(searchConditionPanel, BorderLayout.NORTH);

        searchResultPanel = new SearchResultPanel();// 表格面板
        add(searchResultPanel);
    }

    public void actionPerformed(ActionEvent e) {// 事件处理方法
        if (e.getSource() == searchConditionPanel.searchAll) {// 点击查询全部
            searchResultPanel.draw(InfoQuery.infos);
        } else if (e.getSource() == searchConditionPanel.searchOneDepartment) {// 点击查询部门
            String str = (String) searchConditionPanel.chooseDepartment.getSelectedItem();
            List<String[]> list = new ArrayList<>();
            for (String[] item : InfoQuery.infos) {
                if (item[4].equals(str)) {
                    list.add(item);
                }
            }
            searchResultPanel.draw(list.toArray(new String[0][0]));
        }
    }
}